﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Register.BLL;
using Register.Command;
using Register.DAL;
using Register.Model;
using System.Web.Script.Serialization;
using System.Data.SqlClient;

namespace RegisterWeb.Manager.SystemManager.ashx
{
    /// <summary>
    /// SystemPersonInfoManage 的摘要说明
    /// </summary>
    public class SystemPersonInfoManage : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            string action=context.Request["action"];
            #region 获取个人信息
            if (action.Equals("get")) {

                List<AdminInfo> list = new List<AdminInfo>();
                list = SystemManageBLL.GetAdminInfo("AD20120707004");  //临时模拟赋予一个系统用户ID
                List<AdminInfoJSON> json = new List<AdminInfoJSON>();

                foreach (AdminInfo admin in list)
                    {

                        json.Add(new AdminInfoJSON(HospitalInstallBLL.GetHospitalNameById(admin.Administrator_Hosptial),admin.Administrator_ID,admin.Administrator_Name,admin.Administrator_Password));

                   

                }

                JavaScriptSerializer jss = new JavaScriptSerializer();
                context.Response.Write(jss.Serialize(json));
            
            }
            #endregion
            #region 更新个人信息
            else if (action.Equals("edit")) {
                AdminInfo admin = new AdminInfo();
                admin.Administrator_ID=context.Request["id"];
                admin.Administrator_Name = context.Request["Administrator_Name"];
                admin.Administrator_Password = MD5Provider.Hash(context.Request["Administrator_Password"]);
                admin.Administrator_LastRegTime = DateTime.Now;
                JavaScriptSerializer jss = new JavaScriptSerializer();
                bool flag= SystemManageBLL.UpdateAdminInfo(admin);
                if (flag)
                {
                    context.Response.Write("ok");
                }
                else { context.Response.Write("no"); }
            
            }
            #endregion
            #region 检查个人用户名是否已经存在
            else if (action.Equals("check"))
            {
                string name = context.Request["name"];
                if (SystemManageBLL.Exists(name))
                {
                    context.Response.Write("ok");
                }
                else
                {
                    context.Response.Write("no");
                }
            }


            #endregion
            #region 绑定数据库
            else if (action.Equals("database")) {
                context.Response.Write("[{\"Database_Id\":\"db_register\",\"Database_Name\":\"db_register\"}]");
            }
            #endregion
            #region 备份数据库
            else if (action.Equals("backup")) {
                string operateDatabase = context.Request["operateDatabase"];
                string backupPosition = context.Request["backupPosition"];
                string cmdtxt1 = "Server=.\\SQLEXPRESS;database='" + operateDatabase + "';Uid=sa;Pwd=123";
                string cmdtxt2 = "backup database  " + operateDatabase + "  to disk='" + backupPosition + "/db_register.bak'";
                SqlConnection con = new SqlConnection(cmdtxt1);
                con.Open();  //打开数据库连接
                try
                {
                    SqlCommand com = new SqlCommand(cmdtxt2, con);
                    com.ExecuteNonQuery();
                    context.Response.Write("ok");
                }
                catch (Exception ms)
                {
                    LogRecordHelper.LogNote("ERROR", ms.Message);
                    context.Response.Write("no");
                }
                finally {
                    con.Close();
                }
            }
            #endregion
            #region 检查旧密码是否输入正确
            else if (action.Equals("checkOldPwd"))
            {
                string pwd= MD5Provider.Hash(context.Request["pwd"]);
                string id= context.Request["id"];
                if (SystemManageBLL.ExistsPwd(pwd,id))
                {
                    context.Response.Write("ok");
                }
                else
                {
                    context.Response.Write("no");
                }
            }

            #endregion

        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}
class AdminInfoJSON
{

    public string Administrator_ID
    {
        set;
        get;
    }
    public string Administrator_Name
    {
        get;
        set;
    }
    public string Administrator_Password
    {
        get;
        set;
    }

    public string Administrator_Hosptial
    {
        get;
        set;
    }

    public AdminInfoJSON(string Administrator_Hosptial,string Administrator_ID,string Administrator_Name,string Administrator_Password)
    {
        this.Administrator_Hosptial = Administrator_Hosptial;
        this.Administrator_ID = Administrator_ID;
        this.Administrator_Name = Administrator_Name;
        this.Administrator_Password = Administrator_Password;
    }
}